package org.xu.logic;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import org.xu.utils.ExcelData;
import org.xu.utils.ExcelUtils;

import java.util.List;

@Slf4j
@Service
public class SQLLogicImpl implements SQLLogic{

    @Override
    public void upload(MultipartFile file,String tableName){
        Sheet sheet = ExcelUtils.getExcelSheet(file);
        Assert.notNull(sheet,"sheet is null");
        try {
            ExcelData excelData = ExcelUtils.getListExcelData(sheet);
            log.info("excelData: 表头:{}",excelData.getTableHeads());
            log.info("数据量:{}",excelData.getDataList().size());
            System.out.println("==============================================");
            List<String> tableHeads = excelData.getTableHeads();
            for (int i = 0; i < excelData.getDataList().size(); i++) {
                String str = "UPDATE " + tableName + " SET ";
                Object[] data = excelData.getDataList().get(i);
                for (String tableHead : tableHeads) {
                    String value = data[i] == null ? null : data[i].toString().trim();
                    str = str + tableHead + " = " + value + ",";
                }
                System.out.println(str);
            }
        }catch (Exception e){
            log.info("SQL生成失败:",e);
        }
    }
}
